//
//  Visitor_hasHandledView.m
//  FaceRecognition
//
//  Created by zz on 17/7/6.
//  Copyright © 2017年 Sylar. All rights reserved.
//

#import "Visitor_hasHandledView.h"

@interface Visitor_hasHandledView()

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UILabel *nameLabel; //访客姓名
@property (nonatomic, strong) UILabel *visitedLabel; //被访问者
@property (nonatomic, strong) UILabel *startTimeLabel; //访问开始时间
@property (nonatomic, strong) UILabel *endTimeLabel; //访问结束时间
@property (nonatomic, strong) UILabel *visitorStateLabel; //状态

@property (nonatomic, strong) UILabel *visitorSexLabel; //访客性别
@property (nonatomic, strong) UILabel *visitorUnitLabel; //访客单位
@property (nonatomic, strong) UILabel *phoneLabel; //访客电话

@property (nonatomic, strong) UILabel *visitorTimeLabel; //访问时间

@property (nonatomic, strong) UILabel *visitorLengthLabel; //访问时长

@property (nonatomic, strong) UILabel *visitorLeaveMsgLabel; //访客留言

@property (nonatomic, strong) UILabel *visitedLeaveMsgLabel; //受访留言

@property (nonatomic, strong) UIButton *closeBtn;
@end

@implementation Visitor_hasHandledView {
    
    CGFloat labelW;
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeBtn setImage:[UIImage imageNamed:@"fangke_close"] forState:UIControlStateNormal];
        [_closeBtn addTarget:self action:@selector(tapToHidde) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_closeBtn];
        
        labelW = self.frame.size.width - (33 + 16) * 2;
        
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        [UIButton buttonWithType:UIButtonTypeSystem];
        
        CGFloat x = frame.size.width;
        CGFloat h = frame.size.height;
        
        UITapGestureRecognizer *hiddeTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapToHidde)];
        [self addGestureRecognizer:hiddeTap];
        
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(33, (h - 416) * 0.5, x - 33 * 2, 466)];
        _bgView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_bgView];
        [_bgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgviewTap)]];
        
        _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(16, 28, 100, 100)];
        _imgView.contentMode = UIViewContentModeScaleAspectFill;
        _imgView.clipsToBounds = YES;
        [_bgView addSubview:_imgView];
        
        
        x = CGRectGetMaxX(_imgView.frame);
        
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(x + 21, 36, 200, 16)];
        _nameLabel.font = [UIFont systemFontOfSize:16];
        _nameLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        _nameLabel.text = @"访客姓名: 王新山";
        [_bgView addSubview:_nameLabel];
        
        _visitorSexLabel = [[UILabel alloc] initWithFrame:CGRectMake(x + 21, CGRectGetMaxY(_nameLabel.frame) + 18, 200, 16)];
        _visitorSexLabel.font = [UIFont systemFontOfSize:16];
        _visitorSexLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        _visitorSexLabel.text = @"访客性别: 男";
        _visitorSexLabel.hidden = YES;
        [_bgView addSubview:_visitorSexLabel];
        
        _visitorStateLabel = [[UILabel alloc] initWithFrame:CGRectMake(x + 21, CGRectGetMaxY(_visitorSexLabel.frame) + 18, 200, 16)];
        _visitorStateLabel.font = [UIFont systemFontOfSize:16];
        _visitorStateLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:@"访问状态:已接受"];
        [attrStr addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithHexString:@"#f55f5f"]} range:NSMakeRange(5, 3)];
        _visitorStateLabel.attributedText = attrStr;
        [_bgView addSubview:_visitorStateLabel];
        
        _visitorUnitLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(_imgView.frame) + 22, 300, 16)];
        _visitorUnitLabel.font = [UIFont systemFontOfSize:16];
        _visitorUnitLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        _visitorUnitLabel.text = @"访客单位:新华区教育局";
        [_bgView addSubview:_visitorUnitLabel];
        
        
        _phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(_visitorUnitLabel.frame) + 18, 300, 16)];
        _phoneLabel.font = [UIFont systemFontOfSize:16];
        _phoneLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        _phoneLabel.text = @"联系方式:12345678901";
        [_bgView addSubview:_phoneLabel];
        
        
        _visitorTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(_phoneLabel.frame) + 18, 300, 16)];
        _visitorTimeLabel.font = [UIFont systemFontOfSize:16];
        _visitorTimeLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        _visitorTimeLabel.text = @"访问时间:2017-2018";
        [_bgView addSubview:_visitorTimeLabel];
        
        _visitorLengthLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(_visitorTimeLabel.frame) + 18, 200, 16)];
        _visitorLengthLabel.font = [UIFont systemFontOfSize:16];
        _visitorLengthLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        _visitorLengthLabel.text = @"访问时长:4";
        [_bgView addSubview:_visitorLengthLabel];
        
        _visitorLeaveMsgLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(_visitorLengthLabel.frame) + 18, labelW, 16)];
        _visitorLeaveMsgLabel.font = [UIFont systemFontOfSize:16];
        _visitorLeaveMsgLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        _visitorLeaveMsgLabel.text = @"访客留言:去年买了个表";
        _visitorLeaveMsgLabel.numberOfLines = 0;
        [_bgView addSubview:_visitorLeaveMsgLabel];
        
        _visitedLeaveMsgLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(_visitorLeaveMsgLabel.frame) + 18, labelW, 16)];
        _visitedLeaveMsgLabel.font = [UIFont systemFontOfSize:16];
        _visitedLeaveMsgLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        _visitedLeaveMsgLabel.text = @"受访留言:去年买了个登山包";
        _visitedLeaveMsgLabel.numberOfLines = 0;
        [_bgView addSubview:_visitedLeaveMsgLabel];
    }
    
    return self;
}


- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    _visitorLeaveMsgLabel.height = [_visitorLeaveMsgLabel.attributedText.string boundingRectWithSize:CGSizeMake(labelW, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil].size.height;
    
    _visitedLeaveMsgLabel.y = CGRectGetMaxY(_visitorLeaveMsgLabel.frame) + 18;
    
    _visitedLeaveMsgLabel.height = [_visitedLeaveMsgLabel.attributedText.string boundingRectWithSize:CGSizeMake(labelW, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil].size.height;
    
    _bgView.height = CGRectGetMaxY(_visitedLeaveMsgLabel.frame) + 45;
    _bgView.center = self.center;
    
    
    _closeBtn.frame = CGRectMake(0, 0, 28, 28);
    _closeBtn.center = CGPointMake(CGRectGetMaxX(_bgView.frame), CGRectGetMinY(_bgView.frame));
    [self bringSubviewToFront:_closeBtn];
}

- (void)tapToHidde {
    
    [self removeFromSuperview];
}

- (void)setModel:(FR_VisitorModel *)model {

    _model = model;
    
    //头像
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",REQUESTIMAGEURL,model.photo];
    [_imgView sd_setImageWithURL:[NSURL URLWithString:urlStr]];
    
    _nameLabel.text = [NSString stringWithFormat:@"访客姓名:%@",model.name];
    
    NSMutableAttributedString *attrStr;
    
    if (model.approval == 1) {
        
       attrStr = [[NSMutableAttributedString alloc] initWithString:@"访问状态:已接受"];
        [attrStr addAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#979797"]} range:NSMakeRange(5, attrStr.length - 5)];
    }else if (model.approval == 2) {
        attrStr = [[NSMutableAttributedString alloc] initWithString:@"访问状态:已拒绝"];
        [attrStr addAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#f55f5f"]} range:NSMakeRange(5, attrStr.length - 5)];
    }
    
    _visitorStateLabel.attributedText = attrStr;
    
    //访客单位
    _visitorUnitLabel.text = [NSString stringWithFormat:@"访客单位:%@",model.comefrom];
    
    //联系方式
    _phoneLabel.text = [NSString stringWithFormat:@"联系方式:%@",model.telephone];
    
    //访问时间
    _visitorTimeLabel.text = [NSString stringWithFormat:@"访问时间:%@",model.starttime];
    
    //访问时长
    _visitorLengthLabel.text = [NSString stringWithFormat:@"访问时长:%@",model.timelength];
    
    //访客留言
    attrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"访客留言:%@",model.remark]];
    [attrStr addAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#979797"]} range:NSMakeRange(5, attrStr.length - 5)];
    _visitorLeaveMsgLabel.attributedText = attrStr;
    
    
    if (model.message.length > 0) {
        
        attrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"受访留言:%@",model.message]];
        
        [attrStr addAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#979797"]} range:NSMakeRange(5, attrStr.length - 5)];
    }else {
        
        attrStr = [[NSMutableAttributedString alloc] initWithString:@"受访留言:"];
    }
    
    _visitedLeaveMsgLabel.attributedText = attrStr;
    
}

- (void)bgviewTap {
    
    
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

    UIView *view = [super hitTest:point withEvent:event];
    return view;
}



@end
